package com.egoonet.websocket;

import org.json.JSONObject;

import com.egoonet.msg.RTMsg;

public class MsgHelper {
	/**
	 * 用户被强制退出，其它客户端使用同样用户id登录引起的
	 */
	public static final String STATE_FORCE_LOGOUT = "forceloginout";
	
	/**
	 * 用户登录成功
	 */
	public static final  String STATE_LOGIN = "login";
	
	/**
	 * 用户被锁定
	 */
	public static final String STATE_USER_LOCKED = "locked";
	
	/**
	 * 用户被解锁
	 */
	public static final String STATE_USER_UNLOCKED = "unlocked";
	
	
	/**
	 * 构建登录结果
	 * @param result
	 * @param reason
	 * @return
	 */
	public static String buildLoginResult(boolean result, String reason){
		JSONObject json = new JSONObject();
		json.put("eventName", "login");
		
		JSONObject data = new JSONObject();
		data.put("result", result);
		data.put("reason", reason);
		json.put("data", data);
		
		return json.toString();
	}
	
	/**
	 * 构建强制登出消息
	 * @param reason
	 * @return
	 */
	public static String buildForceLogoutResult(String reason){
		JSONObject json = new JSONObject();
		json.put("eventName", "forcelogout");
		
		JSONObject data = new JSONObject();
		data.put("reason", reason);
		json.put("data", data);
		
		return json.toString();
	}
	
	/**
	 * 构建锁定消息
	 * @param result
	 * @param reason
	 * @return
	 */
	public static String buildLockResult(boolean result, String reason){
		JSONObject json = new JSONObject();
		json.put("eventName", "lock");
		
		JSONObject data = new JSONObject();
		data.put("result", result);
		data.put("reason", reason);
		json.put("data", data);
		
		return json.toString();
	}
	
	/**
	 * 构建解除锁定消息
	 * @param result
	 * @param reason
	 * @return
	 */
	public static String buildUnlockResult(boolean result, String reason){
		JSONObject json = new JSONObject();
		json.put("eventName", "unlock");
		
		JSONObject data = new JSONObject();
		data.put("result", result);
		data.put("reason", reason);
		json.put("data", data);
		
		return json.toString();
	}
	
	
	/**
	 * 构建用户状态
	 * @param userId
	 * @param state
	 * @return
	 */
	public static RTMsg buildUserStatus(String userId, String state){
		RTMsg msg = new RTMsg();
		msg.setEventName("userstatus");

		JSONObject json = new JSONObject();
		json.put("userId", userId);
		json.put("state", state);
		msg.setData(json.toString());
		
		return msg;
	}
}
